package com.cadre.common.utils.oldutil;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* loaded from: RenmbReportHelper.class */
public class RenmbReportHelper {
    private static final int DEFAULT_SIZE = 14;
    private static final int MIN_SIZE = 6;
    private static Map<String, int[]> config = new HashMap();

    static {
        config.put("a01_a1421", new int[]{242, 284, 426, 620});
        config.put("new_203", new int[]{62, 74, 90, 170});
        config.put("a01_a0814", new int[]{46, 54, 74, 148});
        config.put("a01_a0401", new int[]{22, 26, 32, 80});
        config.put("new1002", new int[]{98, 114, 152, 262});
        config.put("a01_a0277", new int[]{98, 114, 152, 262});
        config.put("a01_a0281", new int[]{98, 114, 152, 262});
        config.put("a01_a0101", new int[]{8, 18, 22, 40});
        config.put("a01_a0127", new int[]{8, 18, 22, 40});
        config.put("a01_a0187", new int[]{34, 38, 50, 92});
        config.put("a01_a0324", new int[]{32, 34, 40, 80});
        config.put("a01_a1511", new int[]{118, 212, 338, 446});
        config.put("new_201", new int[]{DEFAULT_SIZE, 18, 22, 40});
        config.put("quanrizhi", new int[]{32, 36, 42, 52});
        config.put("zaizi", new int[]{32, 36, 42, 52});
    }

    public static void setSpacing(XWPFParagraph paragraph, String space) {
        CTPPr pPPr = XWPFHelper.getParagraphCTPPr(paragraph);
        CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() : pPPr.addNewSpacing();
        pSpacing.setLineRule(STLineSpacingRule.EXACT);
        pSpacing.setLine(new BigInteger(space));
    }

    public static void setCellText(XWPFRun run, String str) {
        String[] text = NStringUtils.splitText(str);
        for (int i = 0; i < text.length; i++) {
            run.setText(text[i]);
            if (i + 1 < text.length) {
                run.addBreak();
            }
        }
    }

    public static int executeFontSize(String value, Map<Integer, Integer> map) {
        int fontSize = MIN_SIZE;
        int length = NStringUtils.getWordCount(value);
        Iterator<Integer> it = map.keySet().iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Integer key = it.next();
            if (key.intValue() >= length) {
                fontSize = map.get(key).intValue();
                break;
            }
        }
        return fontSize;
    }

    public static int executeFontSize(String value, String key) {
        if (config.get(key) == null) {
            return DEFAULT_SIZE;
        }
        if (key.startsWith("new201")) {
            key = "new201";
        }
        if (key.startsWith("new203")) {
            key = "new203";
        }
        int[] result = {DEFAULT_SIZE, 12, 10, 8};
        int[] fontList = config.get(key);
        int fontSize = MIN_SIZE;
        int length = NStringUtils.getWordCount(value);
        int i = 0;
        while (true) {
            if (i >= fontList.length) {
                break;
            } else if (fontList[i] >= length) {
                fontSize = result[i];
                break;
            } else {
                i++;
            }
        }
        return fontSize;
    }

    public static boolean isBreak(String value) {
        boolean flag;
        if (value == null || value.equals("")) {
            return false;
        }
        if (value.indexOf("\r\n") > -1 || value.indexOf("\r") > -1 || value.indexOf("\n") > -1) {
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }

    public static String[] splitstr(String value) {
        String[] strs = {"", ""};
        if (value.indexOf("\r\n") > -1) {
            strs = value.split("\r\n");
        } else if (value.indexOf("\r") > -1) {
            strs = value.split("\r");
        } else if (value.indexOf("\n") > -1) {
            strs = value.split("\n");
        }
        return strs;
    }

    public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
        CTBody src1Body = src.getDocument().getBody();
        CTBody src2Body = append.getDocument().getBody();
        List<XWPFPictureData> allPictures = append.getAllPictures();
        Map<String, String> map = new HashMap<>();
        for (XWPFPictureData picture : allPictures) {
            map.put(append.getRelationId(picture), src.addPictureData(picture.getData(), (int) MIN_SIZE));
        }
        appendBody(src1Body, src2Body, map);
    }

    private static void appendBody(CTBody src, CTBody append, Map<String, String> map) throws Exception {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText(optionsOuter);
        String srcString = src.xmlText();
        String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
        String sufix = srcString.substring(srcString.lastIndexOf("<"));
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        if (map != null && !map.isEmpty()) {
            for (Map.Entry<String, String> set : map.entrySet()) {
                addPart = addPart.replace(set.getKey(), set.getValue());
            }
        }
        src.set(CTBody.Factory.parse(prefix + mainPart + addPart + sufix));
    }

    public static void insertNextPageChar(XWPFDocument doc) {
        CTBody body = doc.getDocument().getBody();
        CTSectPr sectPr = doc.createParagraph().getCTP().addNewPPr().addNewSectPr();
        CTSectPr bodySectPr = body.getSectPr();
        sectPr.setPgSz(bodySectPr.getPgSz());
        sectPr.setPgMar(bodySectPr.getPgMar());
        sectPr.setCols(bodySectPr.getCols());
        sectPr.setDocGrid(bodySectPr.getDocGrid());
    }
}
